<?php
require(dirname(__FILE__) .'/check.php');


switch ($mudi){
	case 'getBaoxiaoNum':
		GetBaoxiaoNum();
		break;

	case 'getGuajibaoNum':
		GetGuajibaoNum();
		break;

	case 'getWorkOrderInfo':
		GetWorkOrderInfo();
		break;

	case 'getDialingCity':
		GetDialingCity();
		break;

	default:
		die('err');
}





// 获取最新包销数量
function GetBaoxiaoNum(){
	global $DB,$sysAdminArr;

	$type		= OT::GetStr('type');
	$dataID		= OT::GetInt('dataID');

	switch ($type){
		case 'host':	$tabPart = 'HD_';	$tabName = 'hostData';		break;
		case 'vpsApi':	$tabPart = 'VAD_';	$tabName = 'vpsApiData';	break;
		default :		JS::AlertEnd($type .' 不是有效类型');	break;
	}

	if ($dataID > 0){
		$whereStr = $tabPart .'ID='. $dataID;
	}else{
		$whereStr = $tabPart .'pbaoxiao<>""';
	}

	$proexe = $DB->query('select '. $tabPart .'ID,'. $tabPart .'theme,'. $tabPart .'typeID,'. $tabPart .'pcountry,'. $tabPart .'ptype,'. $tabPart .'pname,'. $tabPart .'pbaoxiao from '. OT_dbPref . $tabName .' where '. $whereStr);
	if (! $rs1 = $proexe->fetch()){
		JS::AlertEnd('没有包销产品，无需该操作');
	}else{
		$todayTime = TimeDate::Get();
		$todayDate = TimeDate::Get('date');
		$retStr = '';
		do{
			$typeexe = $DB->query('select VT_classID,VT_companyID from '. OT_dbPref .'vpsType where VT_ID='. $rs1[$tabPart .'typeID'] .'');
				if (! $rs2 = $typeexe->fetch()){ JS::AlertCloseEnd('搜索不到产品类型['. $rs1[$tabPart .'typeID'] .']'); }

			$comexe = $DB->query('select VC_apiID,VC_apiDomain,VC_apiUsername,VC_apiUserPwd from '. OT_dbPref .'vpsCompany where VC_ID='. $rs2['VT_companyID'] .'');
				if (! $rs3 = $comexe->fetch()){ JS::AlertCloseEnd('搜索不到上级渠道商记录['. $rs2['VT_companyID'] .']'); }

			ApiJingan::$accessId	= $rs3['VC_apiUsername'];
			ApiJingan::$accessKey	= Encrypt::SyEnDe('de',$rs3['VC_apiUserPwd']);
			ApiJingan::$urlMode		= $rs1[$tabPart .'pcountry'];

			if (strpos($rs1[$tabPart .'pbaoxiao'],'|month|') !== false){
				for ($i=1; $i<=2; $i++){
					if ($i == 2){
						$typeStr = $type .'BaoxiaoXu1';
						$typeMode = 1;
						$nameStr = '续费';
					}else{
						$typeStr = $type .'Baoxiao1';
						$typeMode = 0;
						$nameStr = '购买';
					}
					$dataArr = array(
						'yid'		=> 1,
						'ptype'		=> $rs1[$tabPart .'ptype'],
						'pname'		=> $rs1[$tabPart .'pname'],
						'type'		=> $typeMode	// 0购买包销，1续费包销
						);
					$resArr = ApiJingan::GetBaoxiaoNum($dataArr);
					if (! $resArr['res']){
						if (strpos($resArr['note'],':1025}') !== false){
							$bxNum = 0;
						}else{
							JS::AlertEnd('检验包销订单数量出错（'. $resArr['note'] .'）');
						}
					}else{
						$bxNum = intval($resArr['info']);
					}
					$retStr .= $rs1[$tabPart .'theme'] .' 月'. $nameStr .'包销数量（'. $bxNum .'）\n';

					$record = array();
					$record['VR_type']		= $typeStr;
					$record['VR_dataID']	= $rs1[$tabPart .'ID'];
					$record['VR_time']		= $todayTime;
					$record['VR_date']		= $todayDate;
					$record['VR_value']		= $bxNum;

					$chkexe = $DB->query('select VR_ID,VR_value from '. OT_dbPref .'vpsRecord where VR_dataID='. $rs1[$tabPart .'ID'] .' and VR_type="'. $typeStr .'" and VR_date='. $DB->ForTime($todayDate));
					if ($vrow = $chkexe->fetch()){
						$DB->UpdateParam('vpsRecord',$record,'VR_ID='. $vrow['VR_ID']);
					}else{
						$DB->InsertParam('vpsRecord',$record);
					}
					unset($chkexe);
				}
			}
			if (strpos($rs1[$tabPart .'pbaoxiao'],'|year|') !== false){
				for ($i=1; $i<=2; $i++){
					if ($i == 2){
						$typeStr = $type .'BaoxiaoXu12';
						$typeMode = 1;
						$nameStr = '续费';
					}else{
						$typeStr = $type .'Baoxiao12';
						$typeMode = 0;
						$nameStr = '购买';
					}
					
					$dataArr = array(
						'yid'		=> 12,
						'ptype'		=> $rs1[$tabPart .'ptype'],
						'pname'		=> $rs1[$tabPart .'pname'],
						'type'		=> $typeMode	// 0购买包销，1续费包销
						);
					$resArr = ApiJingan::GetBaoxiaoNum($dataArr);
					if (! $resArr['res']){
						if (strpos($resArr['note'],':1025}') !== false){
							$bxNum = 0;
						}else{
							JS::AlertEnd('检验包销订单数量出错（'. $resArr['note'] .'）');
						}
					}else{
						$bxNum = intval($resArr['info']);
					}
					$retStr .= $rs1[$tabPart .'theme'] .' 年'. $nameStr .'包销数量（'. $bxNum .'）\n';

					$record = array();
					$record['VR_type']		= $typeStr;
					$record['VR_dataID']	= $rs1[$tabPart .'ID'];
					$record['VR_time']		= $todayTime;
					$record['VR_date']		= $todayDate;
					$record['VR_value']		= $bxNum;

					$chkexe = $DB->query('select VR_ID,VR_value from '. OT_dbPref .'vpsRecord where VR_dataID='. $rs1[$tabPart .'ID'] .' and VR_type="'. $typeStr .'" and VR_date='. $DB->ForTime($todayDate));
					if ($vrow = $chkexe->fetch()){
						$DB->UpdateParam('vpsRecord',$record,'VR_ID='. $vrow['VR_ID']);
					}else{
						$DB->InsertParam('vpsRecord',$record);
					}
					unset($chkexe);

					if ($type == 'host'){ break; }
				}
			}
		}while ($rs1 = $proexe->fetch());

		JS::AlertEnd($retStr);
	}

}


// 获取挂机宝剩余台数
function GetGuajibaoNum(){
	global $DB;

	$type		= OT::GetStr('type');

	$proexe = $DB->query('select VAD_ID,VAD_theme,VAD_typeID,VAD_comProID from '. OT_dbPref .'vpsApiData where VAD_useMenuStr like "%|guajibao|%"');
	if (! $row = $proexe->fetch()){
		JS::AlertEnd('没有挂机宝产品，无需该操作');
	}else{
		// $todayTime = TimeDate::Get();
		$retStr = '';
		do{
			/*
			$reqArr = ReqUrl::UseAuto(0, 'GET', 'http://api.ipcomserver.com/user/vps-serverID.asp?id='. $row['VAD_comProID'], 'GBK');
			$mjList = str_replace('{"id":"0","name":"null"},', '', Str::GetMark($reqArr['note'], '"list":[', ']}'));
			$num = Str::GetMark($reqArr['note'],'(剩余','台');
			if (strlen($num) == 0 || is_numeric($num) == false){ $num = -1; }else{ $num = intval($num); }
			*/
			/*
			$webHtml = new WebHtml();
			$getStr	= $webHtml->GetCode('http://api.ipcomserver.com/user/vps-serverID.asp?id='. $row['VAD_comProID'],'GBK');
			$mjList	= $webHtml->GetStr($getStr, '"list":[', ']}');
			$mjArrStr = $webHtml->GetArrStr($mjList,'(剩余','台');
			$num = 0;
			$tempArr = explode('[OT]',$mjArrStr);
			$tempCount = count($tempArr);
			for ($i=0; $i<$tempCount; $i++){
				if (strlen($tempArr[$i]) == 0 || is_numeric($tempArr[$i]) == false){
					$num = -1;
					break;
				}else{
					$num += intval($tempArr[$i]);
				}
			}

			$DB->UpdateParam('vpsApiData', array('VAD_guajibaoNum'=>$num,'VAD_guajibaoTime'=>$todayTime), 'VAD_ID='. $row['VAD_ID']);
			*/
			$num = AppVpsApiDeal::GuajibaoNum($row['VAD_ID'], $row['VAD_comProID']);
			$retStr .= $row['VAD_theme'] .' 预计剩余 '. $num .' 台\n';

		}while ($row = $proexe->fetch());

		JS::AlertEnd($retStr);
	}

}


// 获取工单信息（废弃）
function GetWorkOrderInfo(){
	global $DB,$sysAdminArr;

	$newNum = $DB->GetOne('select count(WO_ID) from '. OT_dbPref .'workOrder where WO_state=0');
	$dealNum = $DB->GetOne('select count(WO_ID) from '. OT_dbPref .'workOrder where WO_state>=10 and WO_state<=30');
	echo('
	<!-- 新问题：'. $newNum .'个，处理中：'. $dealNum .'个，共'. ($newNum+$dealNum) .'个 -->
	<span onclick=\'HrefTo("","工单管理","workOrder.php?mudi=manage&dataMode=&dataModeStr=&dataType=&dataTypeCN=%E5%B7%A5%E5%8D%95&dataType2=&dataID=0");\' class="pointer" title="点击进入工单管理">新工单 <span style="font-size:18px;font-weight:bold;color:red;">'. $newNum .'</span> 个，处理中 <span style="font-size:18px;font-weight:bold;color:blue;">'. $dealNum .'</span> 个</span>&ensp;&ensp;'. TimeDate::Get('time') .'
	');
}


// 拨号VPS 地区信息
function GetDialingCity(){
	global $DB;

	$idName		= OT::GetStr('idName');
	$provID		= OT::GetInt('provID');
	$cityID		= OT::GetInt('cityID');
	$defVal		= OT::GetStr('defVal');

	echo('document.getElementById("'. $idName .'").options.length=0;');

	if ($provID <= 0){
		die('document.getElementById("'. $idName .'").options.add(new Option("请先选择省份",""));');
	}

	$dataStr = '';
	$loadexe = $DB->query('select DC_ID,DC_theme from '. OT_dbPref .'dialingCity where DC_provID='. $provID .' and DC_state=1 order by DC_rank ASC');
	while ($row = $loadexe->fetch()){
		$dataStr .= 'document.getElementById("'. $idName .'").options.add(new Option("'. $row['DC_theme'] .'","'. $row['DC_ID'] .'"));';
	}
	unset($loadexe);

	if ($defVal != 'no'){
		if (strlen($dataStr) == 0 && strlen($defVal) == 0){
			echo('document.getElementById("'. $idName .'").options.add(new Option("请先选择省份",""));');
		}else{
			echo('document.getElementById("'. $idName .'").options.add(new Option("'. $defVal .'",""));');
		}
	}

	echo($dataStr);

	if ($cityID > 0){ echo('document.getElementById("'. $idName .'").value = "'. $cityID .'";'); }
}
?>